// pages/user/dingdan.js
//index.js  
//获取应用实例  
var app = getApp();
var common = require("../../utils/common.js");
Page( {  
  data: {  
    winWidth: 0,  
    winHeight: 0,  
    // tab切换  
    currentTab: 0,  
    isStatus:1,//1待付款，2待收货，3已完成
    page:1,
    orderList0:[],
    orderList2:[],
    orderList3:[],
    orderList4:[],
  },  
  onLoad: function(options) { 
     var objectId = options.ming;
  console.log(objectId)
 wx.setNavigationBarTitle(
    {
      title: objectId,
      success: function() {
        console.log('setNavigationBarTitle success')
      },
    })

    this.initSystemInfo();
    this.setData({
      currentTab: 0,
      // parseInt(options.currentTab)
    });
    this.setData({
      isStatus: this.getOrderStatus(),
    });
    console.log(options);

    if(this.data.currentTab == 4){
      this.loadReturnOrderList();
    }else{
      this.loadOrderList();
    }
  },  
  getOrderStatus:function(){
    return this.data.currentTab == 0 ? 1 : this.data.currentTab == 2 ?2 :this.data.currentTab == 3 ? 3:0;
  },
  loadProList: function(){
    var that = this;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Product/index',
      method: 'post',
      data: {
        order_type: that.data.isStatus,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        //--init data        
        var status = res.data.status;
        var list = res.data.ord;
        var winheight = res.data.lenght * 120;
        if (winheight > 850) {
          that.setData({
            height: res.data.lenght * 120,
          });
        } else {
          that.setData({
            height: 850,
          });
        }
        switch (that.data.currentTab) {
          case 0:
            that.setData({
              orderList0: list,
            });
            break;
          case 1:
            that.setData({
              orderList1: list,
            });
            break;
          case 2:
            that.setData({
              orderList2: list,
            });
            break;
          case 3:
            that.setData({
              orderList3: list,
            });
            break;
          case 4:
            that.setData({
              orderList4: list,
            });
            break;
        }
      },
      fail: function () {
        // fail
        wx.showToast({
          title: '网络异常！',
          duration: 2000
        });
      }
    });
  },
  initSystemInfo:function(){
    var that = this;  
  
    wx.getSystemInfo( {
      success: function( res ) {  
        that.setData( {  
          winWidth: res.windowWidth,  
          winHeight: res.windowHeight  
        });  
      }    
    });  
  },
  bindChange: function( e ) {  
  
    var that = this;  
    that.setData( { currentTab: e.detail.current });  
  
  },  
  swichNav: function (e) {
    var that = this;
    if (that.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      var current = e.target.dataset.current;
      that.setData({
        page: 2,
        currentTab: parseInt(current),
        isStatus: e.target.dataset.otype,
      });

      //没有数据就进行加载
      switch (that.data.currentTab) {
        case 0:
          that.loadOrderList();
          break;
        case 1:
          that.loadOrderList();
          break;
        case 2:
          that.loadOrderList();
          break;
        case 3:
          that.loadOrderList();
          break;
        case 4:
          that.loadReturnOrderList();
          break;
      }
    };
  },
  /**
   * 微信支付订单
   */
  payOrderByWechat: function(event){
    var orderId = event.currentTarget.dataset.orderId;
    this.prePayWechatOrder(orderId);
    var successCallback = function(response){
      console.log(response);
    }
    common.doWechatPay("prepayId", successCallback);
  },

  /**
   * 调用服务器微信统一下单接口创建一笔微信预订单
   */
  prePayWechatOrder: function(orderId){
    var uri = "/ztb/userZBT/GetWxOrder";
    var method = "post";
    var dataMap = {
      SessionId: app.globalData.userInfo.sessionId,
      OrderNo: orderId
    }
    console.log(dataMap);
    var successCallback = function (response) {
      console.log(response);
    };
    common.sentHttpRequestToServer(uri, dataMap, method, successCallback);
  },
  onShareAppMessage: function () {
    return {
      title: '微信小程序联盟',
      desc: '最具人气的小程序开发联盟!',
      path: '/page/user?id=123'
    }
  },
})